package com.itguigu.controller;


import com.itguigu.pojo.User;
import com.itguigu.service.UserService;
import com.itguigu.utils.JwtHelper;
import com.itguigu.utils.Result;
import com.itguigu.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtHelper jwtHelper;


    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody User user){

        Result result = userService.login(user);
        return result;

    }


    /**
     * 根据token获取用户信息
     * @param token
     * @return
     */
    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader String token){

        Result result = userService.getUserInfo(token);

        return result;

    }

    /**
     * 查询用户是否存在
     * @param username
     * @return
     */
    @PostMapping("checkUserName")
    public Result checkUserName(@RequestParam String username){


        Result result = userService.checkUser(username);
        return result;

    }


    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("regist")
    public Result Register(@RequestBody User user){


       Result result =  userService.saveOne(user);

       return result;
    }


    /**
     * 登录检查接口
     */

    @GetMapping("checkLogin")
    public Result checkLogin(@RequestHeader String token){

        boolean expiration = jwtHelper.isExpiration(token);

        if(expiration){
            //已经过期
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }

        return Result.ok(null);

    }







}
